package test;

import bean.Teacher;
import dao.TeacherDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.MyBatisUtil;

import java.util.List;

/**
 * @author 飞哥
 * @version V1.0
 * @package test
 * @date 2021/7/8 9:10
 */
public class CRUDGetMapper {
    /*使用getMapper获取代理对象*/
    private TeacherDao teacherDao = MyBatisUtil.getSqlSession().getMapper(TeacherDao.class);

    @Test
    public void select01() {
        /*传统方式*/
        final List<Teacher> teachers = teacherDao.queryTeachers();
        for (Teacher teacher : teachers) {
            System.out.println("老师信息：" + teacher);
        }
        /*现在的方式*/
    }

    @Test
    public void select02() {
        /*传统方式*/
        Teacher teacher = teacherDao.queryTeacherById(1004);
        System.out.println(teacher.toString());
        /*现在的方式*/
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        Teacher selectOne = sqlSession.selectOne("dao.TeacherDao.queryTeacherById", 1004);
        System.out.println(selectOne);
    }

    @Test
    public void insert01() {
        /*传统的方式*/
        /*Teacher t1 = new Teacher();
        t1.setId(1007);
        t1.setName("张三丰");
        t1.setSex("男");
        t1.setEmail("zhangsanfeng@163.com");
        int insertTeacher = teacherDao.insertTeacher(t1);
        System.out.println("成功插入" + insertTeacher + "条数据");*/

        //现在的方式
        Teacher t2 = new Teacher(1008, "张无忌", "男", "zhangwuji@163.com");
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        int insertTeacher02 = sqlSession.insert("dao.TeacherDao.insertTeacher", t2);
        sqlSession.commit();
        System.out.println("成功插入" + insertTeacher02 + "条数据");
    }
}
